<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="id" prop="id">
				<el-input v-model="dataForm.id" placeholder="id"></el-input>
			</el-form-item>
			<el-form-item label="标题" prop="title">
				<el-input v-model="dataForm.title" placeholder="标题"></el-input>
			</el-form-item>
			<el-form-item label="内容" prop="content">
				<el-input v-model="dataForm.content" placeholder="内容"></el-input>
			</el-form-item>
			<el-form-item label="分类id" prop="categoryId">
				<el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
			</el-form-item>
			<el-form-item label="标签id列表" prop="tagIds">
				<el-input v-model="dataForm.tagIds" placeholder="标签id列表"></el-input>
			</el-form-item>
			<el-form-item label="点赞" prop="up">
				<el-input v-model="dataForm.up" placeholder="点赞"></el-input>
			</el-form-item>
			<el-form-item label="踩" prop="down">
				<el-input v-model="dataForm.down" placeholder="踩"></el-input>
			</el-form-item>
			<el-form-item label="喜欢" prop="like">
				<el-input v-model="dataForm.like" placeholder="喜欢"></el-input>
			</el-form-item>
			<el-form-item label="收藏" prop="collect">
				<el-input v-model="dataForm.collect" placeholder="收藏"></el-input>
			</el-form-item>
			<el-form-item label="评论" prop="comment">
				<el-input v-model="dataForm.comment" placeholder="评论"></el-input>
			</el-form-item>
			<el-form-item label="封面" prop="cover">
				<el-input v-model="dataForm.cover" placeholder="封面"></el-input>
			</el-form-item>
			<el-form-item label="状态  0：停用   1：正常" prop="status">
				<el-input v-model="dataForm.status" placeholder="状态  0：停用   1：正常"></el-input>
			</el-form-item>
			<el-form-item label="版本号" prop="version">
				<el-input v-model="dataForm.version" placeholder="版本号"></el-input>
			</el-form-item>
			<el-form-item label="删除标识  0：正常   1：已删除" prop="deleted">
				<el-input v-model="dataForm.deleted" placeholder="删除标识  0：正常   1：已删除"></el-input>
			</el-form-item>
			<el-form-item label="创建者" prop="creator">
				<el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="createTime">
				<el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
			</el-form-item>
			<el-form-item label="更新者" prop="updater">
				<el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
			</el-form-item>
			<el-form-item label="更新时间" prop="updateTime">
				<el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useArticleApi, useArticleSubmitApi } from '@/api/site/article'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	title: '',
	content: '',
	categoryId: '',
	tagIds: '',
	up: '',
	down: '',
	like: '',
	collect: '',
	comment: '',
	cover: '',
	status: '',
	version: '',
	deleted: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: ''
})

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	if (id) {
		getArticle(id)
	}
}

const getArticle = (id: number) => {
	useArticleApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useArticleSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
